// 导入db模块
const db = require('../config/db');
const aes = require('../common/aes');//加密的工具

class Admin {
    // 管理员
    // 登录
    signin(administrator_phone) {
        return new Promise((resolve, reject) => {
            let sql = `select administrator_phone, administrator_pwd,administrator_id,administrator_name,administrator_email,administrator_role from db_administrator where administrator_phone = '${administrator_phone}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            resolve({
                                code: 200,
                                msg: '登录成功。',
                                rows
                            })
                        }
                        else {
                            resolve({
                                code: -1,
                                msg: '账号不存在。',
                                rows
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 注册
    signup(administrator_phone, administrator_pwd, administrator_name, administrator_email) {
        return new Promise((resolve, reject) => {
            let sql = `select administrator_phone from db_administrator where administrator_phone = '${administrator_phone}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length === 0) {
                            // 注册
                            let administrator_role = "administrator"
                            let sql2 = `insert into db_administrator(administrator_phone,administrator_pwd,administrator_name,administrator_email,administrator_role) values('${administrator_phone}','${administrator_pwd}','${administrator_name}','${administrator_email}','${administrator_role}')`;
                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // console.log("x:",result)
                                        if (result.affectedRows === 1) {
                                            resolve({
                                                code: 200,
                                                msg: '注册管理员账号成功。',
                                                administrator_phone,
                                                administrator_name
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '注册管理员账号失败。'
                                            })
                                        }

                                    },
                                    () => {
                                        reject({
                                            code: -1,
                                            msg: '注册管理员账号失败。~~~'
                                        })
                                    }
                                )
                        }
                        else {
                            // 账号已经存在
                            resolve({
                                code: 1,
                                msg: '该管理员账号已经存在。',
                                administrator_phone
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询管理员账号失败。',
                            err
                        })
                    }
                )
        })
    }

    // 管理用户模块
    // 删除账号
    remove_user(user_id) {
        return new Promise((resolve, reject) => {
            // 查询账号
            let sql = `select user_phone from db_user where  user_id='${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 删除数据表的sql语句
                            let sql2 = `delete from db_user where user_id = '${user_id}'`
                            db.query(sql2).then(
                                (result) => {
                                    if (result.affectedRows === 1) {
                                        resolve({
                                            code: 200,
                                            msg: '删除账号成功。',
                                        })
                                    }
                                    else {
                                        resolve({
                                            code: -1,
                                            msg: '删除账号失败。',
                                        })
                                    }
                                },
                                err => {
                                    reject(err)
                                }
                            )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该账户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '查询账号失败。'
                        })
                    }
                )
        })
    }

    // 用户列表
    list_user(page, pageSize) {
        return new Promise((resolve, reject) => {
            //获取分页数据
            let page_num = ((parseInt(page) - 1) * parseInt(pageSize))
            let page_size = (parseInt(pageSize))
            let sql = `select * from db_user limit ${page_num}, ${page_size}`;
            let sql2 = `select count(*) as total from db_user`
            // resolve({page_num, page_size})
            db.query(sql)
                .then(
                    (rows) => {
                        let data;
                        db.query(sql2)
                            .then(
                                (obj) => {
                                    data = obj
                                    resolve({
                                        code: 200,
                                        msg: '获取用户列表成功。',
                                        result: rows,
                                        page,
                                        pageSize,
                                        data
                                    })
                                }
                            )
                    },
                    err => {
                        reject({
                            code: -1,
                            msg: '获取用户列表失败。',
                            err
                        })
                    }
                )
        })
    }

    // 搜索用户
    search(keyword) {
        return new Promise((resolve, reject) => {
            let sql = `select * from db_user where user_phone like '%${keyword}%' or user_name like '%${keyword}%'`;
            db.query(sql)
                .then(
                    (rows) => {
                        resolve({
                            code: 200,
                            msg: `关键字“${keyword}”搜索的结果`,
                            result: rows
                        })
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '关键字搜索失败。'
                        })
                    },
                )
        })
    }

    // 修改用户资料
    modify(user_id, user_name, new_user_pwd) {
        return new Promise((resolve, reject) => {
            let sql = `select user_id from db_user where user_id = '${user_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        if (rows.length == 1) {
                            // 修改用户资料
                            let sql2 = `update db_user set user_name= '${user_name}', user_pwd='${new_user_pwd}' where  user_id= '${user_id}'`;

                            db.query(sql2)
                                .then(
                                    (result) => {
                                        // 判断是否修改成功
                                        if (result.affectedRows == 1) {
                                            resolve({
                                                code: 200,
                                                msg: '修改资料成功。',
                                                user_name
                                            })
                                        }
                                        else {
                                            resolve({
                                                code: -1,
                                                msg: '修改资料失败。'
                                            })
                                        }

                                    },
                                    (err) => {
                                        reject({
                                            code: -1,
                                            msg: '服务端发生异常。~~~',
                                            err
                                        })
                                    }
                                )
                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该用户不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。~~~',
                            err
                        })
                    }
                )
        })
    }

    // 修改管理员资料
    modify_admin(administrator_id, administrator_name, new_admin_pwd, old_admin_pwd) {
        // console.log(administrator_id, administrator_name, new_admin_pwd,old_admin_pwd)
        return new Promise((resolve, reject) => {
            let sql = `select * from db_administrator where administrator_id = '${administrator_id}'`;
            db.query(sql)
                .then(
                    (rows) => {
                        console.log(rows)
                        if (rows.length == 1) {
                            // 判断密码是否正确
                            if (aes.aesDecrypt(old_admin_pwd, rows[0].administrator_pwd)) {
                                // 修改管理员资料
                                let sql2 = `update db_administrator set administrator_name= '${administrator_name}', administrator_pwd='${new_admin_pwd}' where  administrator_id= '${administrator_id}'`;

                                db.query(sql2)
                                    .then(
                                        (result) => {
                                            // 判断是否修改成功
                                            if (result.affectedRows == 1) {
                                                resolve({
                                                    code: 200,
                                                    msg: '修改管理员信息成功。',
                                                    administrator_name
                                                })
                                            }
                                            else {
                                                resolve({
                                                    code: -1,
                                                    msg: '修改管理员信息失败。'
                                                })
                                            }

                                        },
                                        (err) => {
                                            reject({
                                                code: -1,
                                                msg: '服务端发生异常。~~~',
                                                err
                                            })
                                        }
                                    )
                            }
                            else {
                                resolve({
                                    code: -1,
                                    msg: '旧密码不正确~'
                                })
                            }

                        }
                        else {
                            resolve({
                                code: 404,
                                msg: '该管理员不存在。'
                            })
                        }
                    },
                    (err) => {
                        reject({
                            code: -1,
                            msg: '服务端发生异常。~~~',
                            err
                        })
                    }
                )
        })
    }
}
// 暴露Admin类实例
module.exports = new Admin();